package com.covery.config;

import org.quartz.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author ：txw
 * @description：
 * @modified By：
 * @version: 1.0
 * @since ：Created in 2020/5/24 0024 12:12
 */
@Configuration
public class QuartzConfig {
    private final InitSetting initSetting;

    public QuartzConfig(InitSetting initSetting) {
        this.initSetting = initSetting;
    }

    @Bean
    public JobDetail jobDetail1(){
        // copy
        JobDataMap jobDataMap = new JobDataMap();
        jobDataMap.put("initKey", initSetting);
        return JobBuilder.newJob(QuartzJob1.class).setJobData(jobDataMap).storeDurably().build();
    }
    @Bean
    public Trigger trigger1(){
        SimpleScheduleBuilder scheduleBuilder = SimpleScheduleBuilder.simpleSchedule()
                .withIntervalInSeconds(initSetting.getTimeInterval()) //每一秒执行一次
                .repeatForever(); //永久重复，一直执行下去
        return TriggerBuilder.newTrigger()
                .forJob(jobDetail1())
                .withSchedule(scheduleBuilder)
                .build();
    }
}
